﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Core;
using Core.LockStep;

public class MoveSystem : EntitySystem, IStart, IFrameUpdateSystem, IInterpolationUpdateSystem
{
    private IDataEnumerable<StepData1MoveData> moveDatas;
    private IDataEnumerable<BackupData1PlayerData> playerDatas;

    public void Start()
    {
        moveDatas = LockStepTool.GetStepDatas<StepData1MoveData>();
        playerDatas = LockStepTool.GetBackupDatas<BackupData1PlayerData>();
    }

    public void FrameUpdate(int deltaTime)
    {
        foreach (var item in moveDatas)
        {
            var playerData = LockStepTool.GetBackupData<BackupData1PlayerData>(item.PlayerId);

            var speed = 2;
            var moveDelta = item.inputDelta * deltaTime * speed;
            var pos = playerData.position.Current + moveDelta;

            playerData.position.SwapValue(pos);
        }
    }

    public void InterpolationUpdate(float deltaTime)
    {
        foreach (var item in playerDatas)
        {
            if (item.model.Data == null)
                continue;

            item.model.Data.position = item.position.Interpolation.PrecisionV3();
        }

    }
}